using System;
using System.Data;
using PointOfSale.Global;

namespace PointOfSale.Class
{
	public class clsItemDepartment
	{
		private int itemId;
		private int departmentId;
		private double qtyInHand;
		private double maxQty;
		private double minQty;
		private double optQty;

		#region Properties.
		public int ItemId
		{
			get
			{
				return itemId;
			}
			set
			{
				itemId = value;
			}
		}
		public int DepartmentId
		{
			get
			{
				return departmentId;
			}
			set
			{
				departmentId = value;
			}
		}
		public double QtyInHand
		{
			get
			{
				return qtyInHand;
			}
			set
			{
				qtyInHand = value;
			}
		}
		public double MaxQty
		{
			get
			{
				return maxQty;
			}
			set
			{
				maxQty = value;
			}
		}
		public double MinQty
		{
			get
			{
				return minQty;
			}
			set
			{
				minQty = value;
			}
		}
		public double OptQty
		{
			get
			{
				return optQty;
			}
			set
			{
				optQty = value;
			}
		}
		#endregion

		public clsItemDepartment()
		{
			maxQty = 0.0;
			minQty = 0.0;
			optQty = 0.0;
			qtyInHand = 0.0;
			departmentId = clsGlobal.g_DepartmentId;
		}
		public void Add()
		{
			String strInsert = "INSERT tblItemDepartment (ItemId, DepartmentId, MinQty, MaxQty, OptQty)" +
								"VALUES( "		+ "'" +
								itemId			+ "', '" + 
								departmentId	+ "', " +
								maxQty			+ ", " + 
								minQty			+ ", " + 
								optQty			+ ")";
			clsDatabase.ExecuteSQL(strInsert, clsGlobal.g_DataConn);
		}

		public void decreaseAmount(string itemID, string qty, string departmentID)
		{
			string updateCommand = "update tblItemDepartment set optqty = (optqty - " + qty + ") "
				+ " where departmentid = " + departmentID + " and itemid = " + itemID;
			clsDatabase.ExecuteSQL(updateCommand, clsGlobal.g_DataConn);
		}
		public void increaseAmount(string itemID, string qty, string departmentID)
		{
			string updateCommand = "update tblItemDepartment set optqty = (optqty + " + qty + ") "
				+ " where departmentid = " + departmentID + " and itemid = " + itemID;
			clsDatabase.ExecuteSQL(updateCommand, clsGlobal.g_DataConn);
		}
		public static void UpdateQty(String sign, double change, int itemId, int unitId)
		{
			if(IsHandMadeItem(itemId) && (sign == "-"))
			{
				//Tru trong truong hop hang hand made duoc nhap.
				String strSelect = "SELECT ItemId, DepartmentId, UnitId, QtyInHand FROM tblStock WHERE ItemId = " + itemId + " AND UnitId = " + unitId + " AND DepartmentId = " + clsGlobal.g_DepartmentId;
				DataTable dtTemp = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
				if(Convert.ToDouble(dtTemp.Rows[0][3].ToString())>= change)
				{
					String strUpdate = "UPDATE tblStock SET QtyInHand = QtyInHand " + sign + change + " WHERE ItemId = '" + itemId + "'" 
						+ " AND DepartmentId = '" + clsGlobal.g_DepartmentId + "'"
						+ " AND UnitId = '" + unitId + "'";
					clsDatabase.ExecuteSQL(strUpdate, clsGlobal.g_DataConn);
				}
				else//Neu hang nay duoc tao ra tu nguyen vat lieu.
				{
					int stapleId;
					int stapleUnitId;
					double stapleQty;

					//Tim cac nguyen vat lieu tao ra no.
					strSelect = "SELECT StapleId, UnitId, Qty FROM tblStaple WHERE ItemId = " + itemId;
					DataTable dtStaple = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
					int ndtStaple = dtStaple.Rows.Count;
					//Lay thong tin ve tung nguyen vat lieu.
					for (int i = 0; i < ndtStaple; i ++)
					{
						stapleId = Convert.ToInt32(dtStaple.Rows[i][0].ToString());
						stapleUnitId = Convert.ToInt32(dtStaple.Rows[i][1].ToString());
						stapleQty = Convert.ToDouble(dtStaple.Rows[i][2].ToString())* change;

						strSelect = "SELECT UnitId, QtyInHand FROM tblStock WHERE ItemId = " + stapleId + " AND DepartmentId = " + clsGlobal.g_DepartmentId;
						DataTable dtQty = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);

						strSelect = "SELECT UnitSourceId, UnitDestId, Factor FROM tblUnitConversion WHERE ItemId = "  + stapleId;
						DataTable dtUnit = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
					
						clsGlobal.g_IsSubtracted = false;
						UpdateStapleQuantity(dtQty, dtUnit, stapleId, stapleUnitId, stapleQty);
					}
				}
			}
			else//Hang binh thuong.
			{
				String strUpdate = "UPDATE tblStock SET QtyInHand = QtyInHand " + sign + change + " WHERE ItemId = '" + itemId + "'" 
					+ " AND DepartmentId = '" + clsGlobal.g_DepartmentId + "'"
					+ " AND UnitId = '" + unitId + "'";
				clsDatabase.ExecuteSQL(strUpdate, clsGlobal.g_DataConn);
			}
		}
		public static bool IsEnough(int itemId, int unitId, double qty)
		{
			bool isenough = true;
			int stapleId;
			int stapleUnitId;
			double stapleQty;

			//Kiem tra xem co phai la hang dich vu khong ?
			String strCheck = "SELECT IsService FROM tblItem WHERE ItemId = " + itemId;
			DataTable dtTemp = clsDatabase.GetDataToTable(strCheck, clsGlobal.g_DataConn);
			if(Convert.ToBoolean(dtTemp.Rows[0][0].ToString()))
			{
				isenough = true;
			}
			else
			{
				//Neu la hang gia cong..
				if (IsHandMadeItem(itemId))
				{
					//Kiem tra xem no co duoc nhap truoc khi xuat khong
					String strSelect = "SELECT QtyInHand FROM tblStock WHERE " + 
						" ItemId = " + itemId + " AND "    + 
						" UnitId = "  + unitId + " AND "   +
						" DepartmentId = " + clsGlobal.g_DepartmentId + " AND " + 
						" QtyInHand >= " + qty;
					DataTable dt = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
					if(dt.Rows.Count == 1)
					{
						return true;
					}
					else
					{
						//Tim cac nguyen vat lieu tao ra no.
						strSelect = "SELECT StapleId, UnitId, Qty FROM tblStaple WHERE ItemId = " + itemId;
						DataTable dtStaple = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
						int ndtStaple = dtStaple.Rows.Count;
						//Lay thong tin ve tung nguyen vat lieu.
						for (int i = 0; i < ndtStaple && isenough; i ++)
						{
							stapleId = Convert.ToInt32(dtStaple.Rows[i][0].ToString());
							stapleUnitId = Convert.ToInt32(dtStaple.Rows[i][1].ToString());
							stapleQty = Convert.ToDouble(dtStaple.Rows[i][2].ToString())* qty;

							strSelect = "SELECT UnitId, QtyInHand FROM tblStock WHERE ItemId = " + stapleId + " AND DepartmentId = " + clsGlobal.g_DepartmentId;
							DataTable dtQty = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);

							strSelect = "SELECT UnitSourceId, UnitDestId, Factor FROM tblUnitConversion WHERE ItemId = "  + stapleId;
							DataTable dtUnit = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
				
							//Kiem tra xem tung nguyen vat lieu co con du khong ?
							isenough = IsStapleEnough(dtQty, dtUnit, stapleId, stapleUnitId, stapleQty);
							clsGlobal.g_IsEnough = false;//Dung de kiem tra mat hang tiep theo du hay khong.
						}
					}
				}
					//Khong phai la hang gia cong.
				else
				{
					String strSelect = "SELECT QtyInHand FROM tblStock WHERE " + 
						" ItemId = " + itemId + " AND "    + 
						" UnitId = "  + unitId + " AND "   +
						" DepartmentId = " + clsGlobal.g_DepartmentId + " AND " + 
						" QtyInHand >= " + qty;
					DataTable dt = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
					if(dt.Rows.Count != 1)
					{
						isenough = false;
					}
				}
			}
			return isenough;
		}

		public static bool IsHandMadeItem(int itemId)
		{
			String strSelect = "SELECT IsHandMade FROM tblItem WHERE ItemId = " + itemId;
			DataTable dt = clsDatabase.GetDataToTable(strSelect, clsGlobal.g_DataConn);
			return Convert.ToBoolean(dt.Rows[0][0].ToString());
		}

		public static bool IsStapleEnough(DataTable dtQty, DataTable dtUnit, int itemId, int unitId, double qty)
		{
			int ndtQty = dtQty.Rows.Count;
			int ndtUnit = dtUnit.Rows.Count;

			for (int i = 0; i < ndtQty; i ++)
			{
				//Kiem tra xem tai don vi hien tai co du khong.
				if ((dtQty.Rows.Count > 0) && (dtQty.Rows[i][0].ToString() == unitId.ToString()))
				{
					if (qty <= Convert.ToDouble(dtQty.Rows[i][1]))
					{
						clsGlobal.g_IsEnough = true;
					}
					else
					{
						dtQty.Rows.RemoveAt(i);
						ndtQty--;
					}
				}
			}
			if(clsGlobal.g_IsEnough || dtQty.Rows.Count == 0)
			{
				return clsGlobal.g_IsEnough;
			}
			else
			{
				//Neu ma khong du thi chuyen len don vi cha cua no.
				for (int j = 0; j < ndtUnit; j ++)
				{
					if (unitId.ToString() == dtUnit.Rows[j][1].ToString())
					{
						double factor =  Convert.ToDouble(dtUnit.Rows[j][2].ToString());
						qty = qty/factor;
						unitId = Convert.ToInt32(dtUnit.Rows[j][0].ToString());
						j = ndtUnit;
					}
				}
				//De qui qua trinh kiem tra doi voi don vi cha.
				clsGlobal.g_IsEnough = IsStapleEnough(dtQty, dtUnit, itemId, unitId, qty);
			}
			return clsGlobal.g_IsEnough;
		}

		public static void UpdateStapleQuantity(DataTable dtQty, DataTable dtUnit, int itemId, int unitId, double change)
		{
			int ndtQty = dtQty.Rows.Count;
			int ndtUnit = dtUnit.Rows.Count;

			for (int i = 0; i < ndtQty; i ++)
			{
				//Kiem tra xem tai don vi hien tai co du khong.
				if (dtQty.Rows[i][0].ToString() == unitId.ToString())
				{
					if (change <= Convert.ToDouble(dtQty.Rows[i][1]))
					{
						if (!clsGlobal.g_IsSubtracted)
						{
							//Neu du thi tru luon.
							String strUpdate = "UPDATE tblStock SET QtyInHand = QtyInHand - " + change + " WHERE ItemId = '" + itemId + "'" 
								+ " AND DepartmentId = '" + clsGlobal.g_DepartmentId + "'"
								+ " AND UnitId = '" + unitId + "'";
							clsDatabase.ExecuteSQL(strUpdate, clsGlobal.g_DataConn);
							clsGlobal.g_IsSubtracted = true;
						}
					}
					else
					{
						//Neu ma khong du thi chuyen len don vi cha cua no.
						for (int j = 0; j < ndtUnit; j ++)
						{
							if (unitId.ToString() == dtUnit.Rows[j][1].ToString())
							{
								double factor =  Convert.ToDouble(dtUnit.Rows[j][2].ToString());
								double newqty = Math.Ceiling((change - Convert.ToDouble(dtQty.Rows[i][1]))/factor) * factor + Convert.ToDouble(dtQty.Rows[i][1]) - change;
								
								String strUpdate = "UPDATE tblStock SET QtyInHand = " + newqty + " WHERE ItemId = '" + itemId + "'" 
									+ " AND DepartmentId = '" + clsGlobal.g_DepartmentId + "'"
									+ " AND UnitId = '" + unitId + "'";
								clsDatabase.ExecuteSQL(strUpdate, clsGlobal.g_DataConn);
								
								change = Math.Ceiling((change - Convert.ToDouble(dtQty.Rows[i][1]))/factor);
								unitId = Convert.ToInt32(dtUnit.Rows[j][0].ToString());
								j = ndtUnit;
							}
						}
						UpdateStapleQuantity(dtQty, dtUnit, itemId, unitId, change);
					}
				}
			}
		}
	}
}
